package com.huluobo.a2301mdmdemo.demo

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.TypeEvaluator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.PointF
import android.view.animation.AccelerateInterpolator
import android.widget.ImageView
import android.widget.RelativeLayout
import kotlin.random.Random

/**
 *  Created by LC on 2025/7/5.
 */
class BezierLineView(context: Context) : RelativeLayout(context) {
    //需要展示图标的数组
    private lateinit var ids: Array<Int>

    //对外暴露的方法设置心形数组
    fun setIds(ids: Array<Int>) {
        this.ids = ids
    }

    fun addFavor() {
        val iv = ImageView(context)
        //设置一个随机的下标用于展示对应的心形图片
        val index = Random.nextInt(ids.size)
        //设置随机的图片资源
        iv.setImageResource(ids[index])

        val params = LayoutParams(200, 200)
        iv.layoutParams = params
        addView(iv)

        //当前view的宽
        val width = measuredWidth
        //当前view的高
        val height = measuredHeight
        val offset = 100//设置偏移量,当前iv一半的大小

        //起点
        val p0 = PointF(width / 2.0f - offset, height.toFloat())
        //终点
        val p3 = PointF(width / 2.0f - offset, 0f)

        //第一个和第二个偏移量的点,第一个点处于在高度2/3的位置,第二个点处于高度1/3的位置
        val p1 = PointF(((Math.random() * width) - offset).toFloat(), height / 3 * 2f - offset)
        val p2 = PointF(((Math.random() * width) - offset).toFloat(), height / 3 * 1f - offset)

        val animator = ValueAnimator.ofObject(BezierTypeEvaluator(p1, p2), p0, p3)
        animator.duration = 1500
        animator.interpolator = AccelerateInterpolator()
        animator.addUpdateListener {
            val pointF = animator.animatedValue as PointF
            iv.x = pointF.x
            iv.y = pointF.y

            val f = it.animatedFraction
            iv.alpha = 1 - f
        }

        animator.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
                removeView(iv)
            }
        })
        animator.start()
    }

    class BezierTypeEvaluator(var p1: PointF, var p2: PointF) : TypeEvaluator<PointF> {
        override fun evaluate(fraction: Float, p0: PointF?, p3: PointF?): PointF {
            val p = PointF()//声明移动的轨迹

            //三阶贝塞尔曲线计算公式
            if (p0 != null && p3 != null) {
                p.x =
                    p0.x * (1 - fraction) * (1 - fraction) * (1 - fraction) + 3 * p1.x * fraction * (1 - fraction) * (1 - fraction) + 3 * p2.x * fraction * fraction * (1 - fraction) + p3.x * fraction * fraction * fraction

                p.y =
                    p0.y * (1 - fraction) * (1 - fraction) * (1 - fraction) + 3 * p1.y * fraction * (1 - fraction) * (1 - fraction) + 3 * p2.y * fraction * fraction * (1 - fraction) + p3.y * fraction * fraction * fraction
            }

//            //二阶贝塞尔曲线计算公式
//            p.x = p0.x * (1 - fraction) * (1 - fraction)
//            +2 * p1.x * fraction * (1 - fraction)
//            +p3.x * fraction * fraction
//
//            //一阶贝塞尔曲线计算公式
//            p.x = p0.x * (1 - fraction)
//            +p3.x * fraction
            //最终返回出p的移动轨迹
            return p
        }
    }

}